<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Module extends Controller
{
	private $_template_f;
	private $_lang;
	private $_numItem = 20;
	private $_format_zone_xml = 'http://admicro2.vcmedia.vn/adt/cpc/zones.xml';
	private $_folderImageStore = 'uploads/images/';
	private $_script_zone_format = '<div id="ssvzone_%s"></div><script type="text/javascript" src="http://admicro1.vcmedia.vn/cpc/ssvzone_%s.js" id="Script_zone_%s"></script>';
	
	function __construct()
	{
		parent::Controller();
		$this->_lang = $this->mycommon->getLangCode();
		$this->_template_f = $this->config->item('template_f');
		$this->load->language('common', $this->_lang);
		$common_lang = $this->lang->line('common_lang');
		if(!$this->session->userdata('uname') || $this->session->userdata('permitid') > -1)
		{
			redirect(site_url('/login'));
			die();
		}
		else
		{
			$permitid = $this->session->userdata('permitid');
			if(!is_numeric($permitid) || $permitid > 0)
			{
				show_error($common_lang['access_denied']);
				die();
			}
		}
		$this->load->model('module/module_model');
		$this->load->language('common', $this->_lang);
		$this->load->language('module/module', $this->_lang);
		$this->load->library('ftp');
		$this->load->helper('path');
	}
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'Self serving' : $title;
		$header['template_f'] = $this->_template_f;
		$header['username'] = '';
		$header['moduleid'] = '';
		$header['permitid'] = '';
		$header['lang'] = $this->_lang;
		$header['module_name'] = 'home';
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
		$this->load->view($this->_template_f . 'menu_view', $data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
	
	function index()
	{
		$this->view();
	}
	
	function create()
	{
		$data = array();
		
		$data['lang'] = $this->lang->line('module_lang');
		$data['errName'] = '';
		$data['errDir'] = '';
		
		$modulename = strtolower(trim($this->input->post('modulename',true)));
		$moduledir = $this->input->post('moduledir',true);
		$listorder = $this->input->post('listorder',true);
		
		if(isset($_POST['modulename']))
		{
			if($modulename == '')
			{
				$data['errName'] = $data['lang']['enter_modulename'];
			}
			elseif(!$this->module_model->check_modulename_available($modulename))
			{
				$data['errName'] = $data['lang']['modulename_existst'];
			}
			
			$sysdirs = array('admin', 'api', 'cache', 'editor', 'file', 'include', 'install', 'javascript', 'module', 'skin', 'template', 'wap');
			
			if($moduledir == '')
			{
				$data['errDir'] = $data['lang']['enter_moduledir'];
			}
			elseif(!$this->module_model->check_moduledir_available($moduledir))
			{
				$data['errDir'] = $data['lang']['moduledir_existst'];
			}
			elseif(in_array($moduledir, $sysdirs))
			{
				$data['errDir'] = $data['lang']['moduledir_sysdirs'];
			}
			elseif(!mkdir(DT_ROOT.'/'.$moduledir, DIR_WRITE_MODE))
			{
				$data['errDir'] = $data['lang']['moduledir_dir'];
			}
			elseif(!is_writeable(DT_ROOT.'/'.$moduledir))
			{
				$data['errDir'] = $data['lang']['moduledir_write'];
			}
			
			if($data['errDir']=='' && $data['errName']=='')
			{
				$this->module_model->add($modulename,$moduledir,$listorder,0,now());
				redirect(site_url('module?msg='.$data['lang']['create_module_success']));
				die();
			}
			
		}
		$data['modulename'] = $modulename;
		$data['moduledir'] = $moduledir;
		$data['listorder'] = $listorder;
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f . 'module/module_create', $data);
		$this->_loadFooter();
	}
	
	function edit()
	{
		$uid = (int)$this->uri->rsegment(3);
		$module = $this->module_model->get_module_info($uid);
		
		$data = array();
		$data['lang'] = $this->lang->line('module_lang');

		$data['errName'] = '';
		$data['errDir'] = '';
		
		if(isset($_POST['updatemodule']))
		{
			$module['modulename'] = $this->input->post('modulename',true);
			$module['moduledir'] = $this->input->post('moduledir',true);
			$module['listorder'] = $this->input->post('listorder',true);
			
			if($module['modulename'] == '')
			{
				$data['errName'] = $data['lang']['enter_modulename'];
			}
			elseif(!$this->module_model->check_modulename_available($module['modulename'],$uid))
			{
				$data['errName'] = $data['lang']['modulename_existst'];
			}
			
			$sysdirs = array('admin', 'api', 'cache', 'editor', 'file', 'include', 'install', 'javascript', 'module', 'skin', 'template', 'wap');
			
			if($module['moduledir'] == '')
			{
				$data['errDir'] = $data['lang']['enter_moduledir'];
			}
			elseif(!$this->module_model->check_moduledir_available($module['moduledir'],$uid))
			{
				$data['errDir'] = $data['lang']['moduledir_existst'];
			}
			elseif(in_array($module['moduledir'], $sysdirs))
			{
				$data['errDir'] = $data['lang']['moduledir_sysdirs'];
			}
			elseif(!mkdir(DT_ROOT.'/'.$module['moduledir'], DIR_WRITE_MODE))
			{
				$data['errDir'] = $data['lang']['moduledir_dir'];
			}
			elseif(!is_writeable(DT_ROOT.'/'.$module['moduledir']))
			{
				$data['errDir'] = $data['lang']['moduledir_write'];
			}
				
			if($data['errName']=='' && $data['errDir']=='')
			{
				$this->module_model->update($uid,$module['modulename'],$module['moduledir'],$module['listorder']);
				redirect(site_url('module?msg='. $data['lang']['update_module_success']));
				die();
			}
		}
		
		$data['modulename'] = $module['modulename'];
		$data['moduledir'] = $module['moduledir'];
		$data['listorder'] = $module['listorder'];
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f . 'module/module_edit', $data);
		$this->_loadFooter();
	}
	
	function delete()
	{
		$data = array();
		$data['lang'] = $this->lang->line('module_lang');
		$uid = (int)$this->uri->rsegment(3);
		$module = $this->module_model->get_module_info($uid);
		if($module['module_id']){
			$this->module_model->delete($uid);
			redirect(site_url('module?msg='. $data['lang']['delete_module_success']));
			die();
		}	
	}
	function active()
	{
		$data = array();
		$data['lang'] = $this->lang->line('module_lang');
		$uid = (int)$this->uri->rsegment(3);
		$status = (int)$this->uri->rsegment(4);
		$module = $this->module_model->get_module_info($uid);
		if($module['module_id']){
			$this->module_model->active($uid,$status);
			redirect(site_url('module?msg='. $data['lang']['update_module_success']));
			die();
		}	
	}
	
	function view()
	{
		$type = $this->input->get('type');
		$level = $this->input->get('level')=='' ? -2 : $this->input->get('level');
		$keyword = trim($this->input->get('keyword'));
		$cPage = $this->input->get('cpage');

		
		$type = ($type == '') ? 'active' : $type;
		$cPage = (is_numeric($cPage) && $cPage > 0) ? $cPage : 1;
		$start = ($cPage - 1) * $this->_numItem;
		$cmofsale = trim($this->input->get('cmofsale',true));
		
		$data = array();
		$dat['lang'] = $this->_lang;
		$data['module_lang'] = $this->lang->line('module_lang');
		// load header
		$this->_loadHeader($data['module_lang']['module_title']);
		
		// load menu
		$this->_loadMenu();
		
		// load content
		$data['lang'] = $this->lang->line('module_lang');
		$lstModuleInfo = $this->module_model->get_all($type, $level, $start, $this->_numItem,$keyword);
		$data['lstModule'] = $lstModuleInfo['lstModule'];
		$data['numCurrItem'] = count($data['lstModule']);
		$this->load->view($this->_template_f . 'module/module_view', $data);
	}
	
}